//
//  CustomPickerViewController.m
//  07picker
//
//  Created by Rocky Xu on 12-8-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CustomPickerViewController.h"

@implementation CustomPickerViewController

@synthesize picker;
@synthesize label;
@synthesize column1;
@synthesize column2;
@synthesize column3;
@synthesize column4;
@synthesize column5;
@synthesize button;
@synthesize crunchSoundID;
@synthesize winSoundID;

-(void)showButton{
    button.hidden = NO;
    
}

-(void)playWinSound{
    AudioServicesPlaySystemSound(winSoundID);
    label.text = @"WIN!";
    [self performSelector:@selector(showButton) withObject:nil afterDelay:1.5];
}

-(void)rollPicker{
    [picker selectRow:1 inComponent:1 animated:YES];
    [picker reloadComponent:1];
}

-(IBAction)spin{
    BOOL win = NO;
    int numInRow = 1;
    int lastVal = -1;
    for (int i=0; i<5; i++) {
        int newValue = random() %[self.column1 count];
        if(newValue == lastVal)
            numInRow++;
        else
            numInRow = 1;
        
        lastVal = newValue;
//        [picker selectRow:1 inComponent:i animated:YES];
//        [picker reloadComponent:i];
//        [picker selectRow:2 inComponent:i animated:YES];
//        [picker reloadComponent:i];
//        [picker selectRow:3 inComponent:i animated:YES];
//        [picker reloadComponent:i];
//        [picker selectRow:4 inComponent:i animated:YES];
//        [picker reloadComponent:i];
//        [picker selectRow:5 inComponent:i animated:YES];
//        [picker reloadComponent:i];
//        [picker selectRow:1 inComponent:i animated:YES];
//        [picker reloadComponent:i];
        //	[self performSelector:@selector(rollPicker) withObject:nil afterDelay:4]; 
        [picker selectRow:newValue inComponent:i animated:YES];
        [picker reloadComponent:i];
        if(numInRow >= 2)
            win = YES;
    
    }
    button.hidden = YES;
    AudioServicesPlaySystemSound(crunchSoundID);
    
    if(win)
        [self performSelector:@selector(playWinSound) withObject:nil afterDelay:0.5];
    else
        [self performSelector:@selector(showButton) withObject:nil afterDelay:.5];
    
    label.text = @"";
//    if(win)
//        label.text=@"win!";
//    else
//        label.text=@"!";
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    UIImage *seven = [UIImage imageNamed:@"seven.png"];
    UIImage *bar = [UIImage imageNamed:@"bar.png"];
    UIImage *crown = [UIImage imageNamed:@"crown.png"];
    UIImage *cherry = [UIImage imageNamed:@"cherry.png"];
    UIImage *lemon = [UIImage imageNamed:@"lemon.png"];    
    UIImage *apple = [UIImage imageNamed:@"apple.png"];
    
    for(int i=1;i<=5;i++){
        UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
        UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
        UIImageView *crownView = [[UIImageView alloc] initWithImage:crown];
        UIImageView *cherryView = [[UIImageView alloc] initWithImage:cherry];
        UIImageView *lemonView = [[UIImageView alloc] initWithImage:lemon];
        UIImageView *appleView = [[UIImageView alloc] initWithImage:apple];
        
        NSArray *imgViewArray =[[NSArray alloc]initWithObjects:sevenView,barView,crownView,cherryView,lemonView,appleView, nil];
        NSString *fieldName = [[NSString alloc]initWithFormat:@"column%d",i];
        [self setValue:imgViewArray forKey:fieldName];
        [fieldName release];
        [imgViewArray release];
        
        [sevenView release];
        [barView release];
        [crownView release];
        [cherryView release];
        [lemonView release];
        [appleView release];
        
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"];
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &winSoundID);
    path = [[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"];
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &crunchSoundID);
    
    srandom(time(NULL));
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    self.picker = nil;
    self.label = nil;
    self.column1 = nil;
    self.column2 = nil;
    self.column3 = nil;
    self.column4 = nil;
    self.column5 = nil;
    self.button = nil;
    
    if(winSoundID){
        AudioServicesDisposeSystemSoundID(winSoundID),        winSoundID = 0;
    }
    if(crunchSoundID)
        AudioServicesDisposeSystemSoundID(crunchSoundID),crunchSoundID = 0;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc{
    [picker release];
    [label release];
    [column1 release];
    [column2 release];
    [column3 release];
    [column4 release];
    [column5 release];
    [button release];
    if(winSoundID){
        AudioServicesDisposeSystemSoundID(winSoundID),        winSoundID = 0;
    }
    if(crunchSoundID)
        AudioServicesDisposeSystemSoundID(crunchSoundID),crunchSoundID = 0;
    [super dealloc];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 5;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.column1 count];
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d",component+1];
    NSArray *array =[self valueForKey:arrayName];
    [arrayName release];
    return [array objectAtIndex:row];
    
}
@end
